package org.shortlink.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.shortlink.admin.common.convention.exception.ClientException;
import org.shortlink.admin.common.enums.UserErrorCodeEnum;
import org.shortlink.admin.dao.entity.UserDO;
import org.shortlink.admin.dao.mapper.UserMapper;
import org.shortlink.admin.dto.resp.UserRespDTO;
import org.shortlink.admin.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户接口实现层
 */
@Service
public class UserService extends ServiceImpl<UserMapper,UserDO> implements IUserService {

    @Override
    public UserRespDTO selectUserByName(String username) {
        UserRespDTO result = new UserRespDTO();

        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<UserDO>();
        queryWrapper.eq(UserDO::getUsername, username);
        UserDO userDO = this.getOne(queryWrapper);

        if(userDO==null){
            throw new ClientException(UserErrorCodeEnum.USER_NULL);
        }

        //将userDO的内容复制到userRespDTO
        BeanUtils.copyProperties(userDO,result);

        return result;
    }
}
